package com.example.lg.aliplaydemo470.activity;

import android.content.Context;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.OrientationHelper;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;

import com.bumptech.glide.Glide;
import com.example.lg.aliplaydemo470.R;
import com.example.lg.aliplaydemo470.bean.VideoBean;
import com.example.lg.aliplaydemo470.interfac.OnViewPagerListener;
import com.example.lg.aliplaydemo470.utils.DataUtil;
import com.example.lg.aliplaydemo470.view.ViewPagerLayoutManager;

import java.util.ArrayList;
import java.util.List;

import cn.jzvd.JZUtils;
import cn.jzvd.Jzvd;
import cn.jzvd.JzvdStd;


/**
 * https://github.com/DingMouRen/LayoutManagerGroup
 * <p>
 * 这篇博客讲的很不错：
 * <p>
 * Android 进阶】仿抖音系列之翻页上下滑切换视频（四）
 * 地址： https://www.jianshu.com/p/e0bd595d6321
 */

public class DouYinVPJZPlayActivity extends AppCompatActivity {
    private static final String TAG = "DouYinVPJZPlayActivity";

    private RecyclerView mRecyclerView;
    private MyAdapter mAdapter;
    private ViewPagerLayoutManager mLayoutManager;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_dou_yin_video_play);

        initView();

        initListener();

    }


    private void initView() {
        mRecyclerView = findViewById(R.id.recycler);

        mLayoutManager = new ViewPagerLayoutManager(this, OrientationHelper.VERTICAL);
        mAdapter = new MyAdapter(this);
        mRecyclerView.setLayoutManager(mLayoutManager);
        mRecyclerView.setAdapter(mAdapter);
    }

    private int currentPosition;

    private void initListener() {
        mLayoutManager.setOnViewPagerListener(new OnViewPagerListener() {

            @Override
            public void onInitComplete() {
                Log.i(TAG, "OnViewPager onInitComplete");
                View itemView = mRecyclerView.getChildAt(0);
                final JzvdStd videoView = itemView.findViewById(R.id.video_view);
                videoView.startVideo();
            }

            @Override
            public void onPageRelease(boolean isNext, int position) {
                Log.i(TAG, "OnViewPager 释放位置:" + position + " 下一页:" + isNext);


            }

            @Override
            public void onPageSelected(int position, boolean isBottom) {
                Log.i(TAG, "OnViewPager 选中位置:" + position + "  是否是滑动到底部:" + isBottom);
                if (currentPosition != position) {
                    Jzvd.releaseAllVideos();
                    View itemView = mRecyclerView.getChildAt(0);
                    int childCount = mRecyclerView.getChildCount();
                    Log.i(TAG, "onPageSelected: childCount=" + childCount);
                    final JzvdStd videoView = itemView.findViewById(R.id.video_view);
                    videoView.startVideo();

                    //videoView.isSaveFromParentEnabled();
                    //videoView.setSaveEnabled(true);
                    //videoView.setDrawingCacheEnabled(true);
                    //JzvdStd.SAVE_PROGRESS = true;//保存进度

                    //设置屏幕常亮
                    JZUtils.scanForActivity(DouYinVPJZPlayActivity.this).getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);


                } else {
                    Log.i(TAG, "OnViewPager onPageSelected: 不做处理");
                }

                currentPosition = position;


            }


        });
    }


    List<VideoBean> videoPathList = new ArrayList<>();
    //private int[] videos = {R.raw.video_1, R.raw.video_2};


    class MyAdapter extends RecyclerView.Adapter<MyAdapter.ViewHolder> {

        Context mContext;

        public MyAdapter(Context mContext) {
            videoPathList = DataUtil.getVideoInfo();

            this.mContext = mContext;
        }


        @Override
        public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
            View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_view_pager, parent, false);
            return new ViewHolder(view);
        }

        @Override
        public void onBindViewHolder(ViewHolder holder, int position) {
            holder.mVideoView.setUp(videoPathList.get(position).getVideoUrl(), "", Jzvd.SCREEN_NORMAL);
            Glide.with(mContext).load(videoPathList.get(position).getVideoCoverUrl()).into(holder.mVideoView.thumbImageView);


        }

        @Override
        public int getItemCount() {
            return videoPathList.size();
        }

        public class ViewHolder extends RecyclerView.ViewHolder {
            JzvdStd mVideoView;

            public ViewHolder(View itemView) {
                super(itemView);
                mVideoView = itemView.findViewById(R.id.video_view);
            }
        }

    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        JzvdStd.releaseAllVideos();

    }

    /*private void playVideo(int position) {
        View itemView = mRecyclerView.getChildAt(0);
        final JzvdStd videoView = itemView.findViewById(R.id.video_view);
        Glide.with(DouYinVPJZPlayActivity.this).load(imgs[position % 2]).into(videoView.thumbImageView);
        videoView.startVideo();


    }*/


   /* private void releaseVideo(int index) {
        // View itemView = mRecyclerView.getChildAt(index);
        // final JzvdStd videoView = itemView.findViewById(R.id.video_view);
        Jzvd.releaseAllVideos();


    }*/
}
